/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Windows.Forms;
using DC;
using MediaDC.Native;

namespace MediaDC
{
    class DirectoryItem : FileListItem
    {
        private readonly DcDirectory _directory;

        public DirectoryItem(DcDirectory directory)
            : base(directory.Name)
        {
            _directory = directory;
            SubItems.Add("Directory");
            ImageIndex = NativeImageList.DirectoryIconIndex;
        }

        public DcDirectory Directory
        {
            get { return _directory; }
        }
    }

    class FileItem : FileListItem
    {
        private readonly DcFile _file;

        public FileItem(DcFile file)
            : base(file.Name)
        {
            _file = file;

            string fileType = "File";
            if (_file.Name.Contains("."))
            {
                fileType = Path.GetExtension(_file.Name);
                ImageIndex = NativeImageList.FileIconIndex(_file.Name);
            }
            SubItems.Add(fileType);
            SubItems.Add(FormatFileSize(_file.Size));
            SubItems.Add(_file.TTH);
        }

        public DcFile File
        {
            get { return _file; }
        }

        public static string FormatFileSize(long size)
        {
            string[] suffixes = new string[]{"B", "KB", "MB", "GB", "TB"};
            const long S = 1024;
            double value = 0;
            string suffix = suffixes[0];

            for (int i = 0; i < suffixes.Length; i++)
            {
                double tmpValue = size / Math.Pow(S, i);
                if (tmpValue < 1)
                    break;

                value = tmpValue;
                suffix = suffixes[i];
            }
            return Math.Round(value, 2) + " " + suffix;
        }
    }

    class FileListItem : ListViewItem
    {
        public FileListItem(string name)
        {
            Name = name;
            SubItems[0].Text = Name;
        }
    }

    class NickListItem : ListViewItem
    {
        private readonly string _nick;
        private long _shareSize;
        private string _description;
        private string _clientInfo;
        private string _connectionType;
        private string _email;
        private bool _isOp;
        private string _ip;

        public NickListItem(string nick)
        {
            _nick = nick;
            Name = nick;
            Text = nick;

            for (int c = 1; c < 7; c++)
                SubItems.Add(string.Empty);
            ImageIndex = 0;
        }

        public void Update(MyInfoEventArgs info)
        {
            _shareSize = info.ShareSize;
            _description = info.Description;
            _clientInfo = info.ClientInfo;
            _connectionType = info.ConnectionType;
            _email = info.Email;

            SubItems[1].Text = FileItem.FormatFileSize(_shareSize);
            SubItems[2].Text = _description;
            SubItems[3].Text = _clientInfo;
            SubItems[4].Text = _email;
            SubItems[5].Text = _connectionType;
            SubItems[6].Text = _ip;
        }

        public string Nick
        {
            get { return _nick; }
        }

        public long ShareSize
        {
            get { return _shareSize; }
        }

        public string Description
        {
            get { return _description; }
        }

        public string ClientInfo
        {
            get { return _clientInfo; }
        }

        public string ConnectionType
        {
            get { return _connectionType; }
        }

        public string Email
        {
            get { return _email; }
        }

        public bool IsOp
        {
            get { return _isOp; }
            set
            {
                _isOp = value;
                ImageIndex = _isOp ? 1 : 0;
            }
        }

        public string Ip
        {
            get { return _ip; }
            set
            {
                _ip = value;
                SubItems[6].Text = _ip;
            }
        }
    }
}
